#include<iostream>
#include<vector>
using namespace std;

template<typename T>
class varray {
private:
	int vsize;
	T* addr;
	int capasity;//实际空间大小
public:
	varray():vsize(0),addr(new T[5]), capasity(5) {}
	varray(int n, T x); 
	varray(const varray&);
	~varray() { delete[] addr; }

	varray& operator=(varray);//重载赋值
	int size()const { return vsize; }
	bool empty()const { return vsize == 0; }
	void push_back(const T& a);//添加元素
	bool pop_back();//删除元素,失败返回false
	bool erease(int i);//根据下标删除元素
	//bool clean() { vsize = 0; }
	T& operator[](int i);

};

template<typename T>
void varray<T>::push_back(const T& a){
	if (vsize >= capasity) {
		capasity << 1;
		cout << capasity << endl;
		T* temp = new int[capasity];
		for (int i = 0; i < vsize; i++) { temp[i] = addr[i]; }
		delete [] addr;
		addr = temp;
	}
	addr[vsize++] = a;
	//cout << addr[vsize-1]<<endl;
}

template<typename T>
varray<T>::varray(int n, T x):addr(new T[n + 5]), vsize(n), capasity(n + 5)
{
	for (int i = 0; i < vsize; i++) addr[i] = x;
}

template<typename T>
varray<T>::varray(const varray<T>& a){
	if (&a == this) return;
	vsize = a.vsize;
	capasity = a.capasity;
	addr = new T[a.capasity];
	for (int i = 0; i < vsize; ++i) { addr[i] = a.addr[i]; }
}
template<typename T>
T& varray<T>::operator[](int i){
	if (i < 0 || i >= vsize) exit(-3);
	return addr[i];
}

template<typename T>
bool varray<T>::pop_back() {
	if (vsize <= 0) return false;
	--vsize;
	return true;
}
template<typename T>
bool varray<T>::erease(int ii)
{	if(ii<0||ii>=vsize)
	return false;
	for (int i = ii; i < vsize-1; i++) {
	swap(addr[i], addr[i + 1]);
	}
	--vsize;
	return true;
}
template<typename T>
varray<T>& varray<T>::operator=(varray a) {
	swap(a.addr, addr);
	swap(a.vsize, vsize);
	swap(a.capasity, capasity);
	return *this;
}

int main() {
	varray<int> test1, test2(3, 3);//测试两个构造函数
	for (int i = 0; i < test2.size(); i++) cout << test2[i];//
	cout << endl;

	test1 = test2;//赋值
	test1.push_back(4);
	for (int i = 0; i < test1.size(); i++) cout << test1[i];
	cout << endl;

	varray<int> test3(test1);
	//test3.pop_back();
	test3[0] = 0;//通过重载的[]赋值
	test3.erease(1);
	for (int i = 0; i < test3.size(); i++) cout << test3[i];
}
